import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('@/views/Login')
const Reg = () => import('@/views/Reg')
const Layout = () => import('@/views/Layout')
const Home = () => import('@/views/Layout/Home')
const ArtCategory = () => import('@/views/Layout/ArtCategory')
const ArtList = () => import('@/views/Layout/ArtList')
const UserInfo = () => import('@/views/Layout/UserInfo')
const ChangeAvatar = () => import('@/views/Layout/ChangeAvatar')
const ResetPwd = () => import('@/views/Layout/ResetPwd')

Vue.use(VueRouter)

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', component: Home },
        { path: '/artcategory', component: ArtCategory },
        { path: '/artlist', component: ArtList },
        { path: '/userinfo', component: UserInfo },
        { path: '/changeavatar', component: ChangeAvatar },
        { path: '/resetpwd', component: ResetPwd }
      ]
    },
    { name: 'login', path: '/login', component: Login },
    { name: 'reg', path: '/reg', component: Reg }
  ]
})

// 路由守卫 控制用户访问权限
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    if (window.localStorage.getItem('token')) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

export default router
